﻿using System;
using System.Web.UI.WebControls;
using Olympic_email_to_future.Module;
using Olympic_email_to_future.IBLL;
using Olympic_email_to_future.BLLFactory;

namespace FutureMeWeb.FutureMe_Admin
{
    public partial class email : System.Web.UI.Page
    {
        public IMailBLL mail = BusinessFactory.CreateEmailBLL();
        public MailInfo mailinfo = new MailInfo();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bind();
            }
        }
        protected void GridView2_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView2.EditIndex = e.NewEditIndex;
            bind();
        }

        //删除
        protected void GridView2_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

            int id = Convert.ToInt32(GridView2.Rows[e.RowIndex].Cells[0].Text);
            mail.deleteMialInfoByEmaiId(id);
            
            GridView2.EditIndex = -1;
            bind();
        }

        //更新
        protected void GridView2_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {

            int id = Convert.ToInt32(GridView2.Rows[e.RowIndex].Cells[0].Text);
            mail.updatePublic(id);
            
            GridView2.EditIndex = -1;
            bind();
        }

        //取消
        protected void GridView2_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView2.EditIndex = -1;
            bind();
        }

        //绑定
        public void bind()
        {

            GridView2.DataSource = mail.dogetMailInfoByPublic();
            GridView2.DataBind();
        }
        protected void GridView2_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView2.PageIndex = e.NewPageIndex;
            bind();
        }

        protected void GvDataRowToolTip(GridViewRowEventArgs e, int id)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[id].ToolTip = e.Row.Cells[id].Text;
                if (e.Row.Cells[id].Text.Length > 5) e.Row.Cells[id].Text = e.Row.Cells[id].Text.Substring(0, 5) + "......";
            }
        }

        protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            GvDataRowToolTip(e, 0);
            GvDataRowToolTip(e, 3);
            GvDataRowToolTip(e, 4);
            GvDataRowToolTip(e, 7);
            GvDataRowToolTip(e, 8);

            string type;
            //如果是绑定数据行  
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //鼠标经过时，行背景色变 
                e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#E6F5FA'");
                //鼠标移出时，行背景色变 
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#FFFFFF'");

                if (e.Row.Cells[3].Text == "0" || e.Row.Cells[3].Text == "1")
                {
                    int x = Convert.ToInt32(e.Row.Cells[3].Text);//第4列
                    if (x == 0)
                    {
                        type = "未发送";

                    }
                    else
                    {
                        type = "已发送";
                    }
                    e.Row.Cells[3].Text = type;
                }
                if (e.Row.Cells[4].Text == "0" || e.Row.Cells[4].Text == "1")
                {
                    int x = Convert.ToInt32(e.Row.Cells[4].Text);//第4列
                    if (x == 1)
                    {
                        type = "Public";

                    }
                    else
                    {
                        type = "Private";
                    }
                    e.Row.Cells[4].Text = type;
                }
                //当有编辑列时，避免出错，要加的RowState判断 
                /*if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
                {
                    ((LinkButton)e.Row.Cells[11].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除这封邮件吗?')");
                }*/

            }

        }

    }
}